import java.util.*;



import java.lang.Thread;
// super this 均不能引用static private等定义的成员 变量
public class Test04 {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        SellticketTask sellticket=new SellticketTask(); //初始化任务
        Thread s1=new Thread(sellticket);                  //创建线程1
        s1.setName("售票点1");
        Thread s2=new Thread(sellticket);                  //创建线程2
        s2.setName("售票点2");

        System.out.println("开始售票");
        System.out.println("售票点	票号	剩余票数");
        s1.start();
        s2.start();


    }
}


//售票
class SellticketTask implements Runnable{
    static int Ticket_num=100;  //保持票数统一 票数要静态
    public volatile boolean exit = false;
    int temp;
    int flag[]=new int[101];
    Random rand = new Random();
    public SellticketTask(){
        for(int i=0;i<101;i++) {
            flag[i]=-1;
        }
    }

    public  void run() {
        while(Ticket_num>0) {
            synchronized(this) {
                if(Ticket_num>0) {
                    while(true) {
                        temp=(int) (rand.nextInt(100)+1);
                        if(flag[temp]!=0) {
                            flag[temp]=0;
                            break;
                        }
                    }

                    System.out.println(Thread.currentThread().getName()+":	"+temp+"	"+--Ticket_num);
                    try{
                        Thread.sleep(new Random().nextInt(100));
                    }catch(Exception e) {
                        e.printStackTrace();
                    }

                }

            }
        }



    }

}

